//
// cxxrts.cpp
// Usage: C++ run time support
// Create Date: 2008-02-13
// Creator: MaJiuyue
//
#include <NewLife.h>
#include <stdlib.h>
#include "bootutil.h"

/* Support for global/static objects */
extern "C"
{
	int __cxa_atexit(void (*f)(void *), void *p, void *d);
	void __cxa_finalize(void *d);
};

struct object
{
	void (*f)(void*);
	void *p;
	void *d;
} object[32] = {0};
unsigned int iObject = 0;

int __cxa_atexit(void (*f)(void *), void *p, void *d)
{
	if(iObject>=32)
		return -1;
	object[iObject].f = f;
	object[iObject].p = p;
	object[iObject].d = d;
	++iObject;
	return 0;
}

void __cxa_finalize(void *d)
{
	unsigned int i = iObject;
	for(; i>0; --i)
	{
		--iObject;
		object[iObject].f(object[iObject].p);
	}
}

void * __dso_handle = &__dso_handle;

void * operator new(unsigned long size) { return malloc(size); }
void operator delete(void *p) { free(p); }
void * operator new[](unsigned long size) { return malloc(size); }
void operator delete[](void *p) { free(p); }


extern "C" void __cxa_pure_virtual()
{
	panic("__cxa_pure_virtual()");
}

namespace std
{
   class type_info
   {
      private:
         const char* tname;

      public:
         virtual ~type_info ( void ) {};
         type_info ( const type_info& arg) : tname(arg.tname) {}
         explicit type_info ( const char* pname) : tname(pname) {}
         const char* name ( void ) const { return tname; }
         bool operator == ( const type_info& arg ) const { return tname==arg.tname; }
         bool operator != ( const type_info& arg ) const { return tname!=arg.tname; }
   };
}

namespace __cxxabiv1
{

   #define ADD_CXX_TYPEINFO_HEADER(t) \
      class t : public std::type_info \
      { \
         public: \
            virtual ~t ( void ) {} \
            explicit t ( const char* n) : std::type_info(n) {} \
      }; \

   ADD_CXX_TYPEINFO_HEADER ( __fundamental_type_info );
   ADD_CXX_TYPEINFO_HEADER ( __array_type_info );
   ADD_CXX_TYPEINFO_HEADER ( __function_type_info );
   ADD_CXX_TYPEINFO_HEADER ( __enum_type_info );
   ADD_CXX_TYPEINFO_HEADER ( __pbase_type_info );
   ADD_CXX_TYPEINFO_HEADER ( __pointer_type_info );
   ADD_CXX_TYPEINFO_HEADER ( __pointer_to_member_type_info );
   ADD_CXX_TYPEINFO_HEADER ( __class_type_info );
   ADD_CXX_TYPEINFO_HEADER ( __si_class_type_info );
   ADD_CXX_TYPEINFO_HEADER ( __vmi_class_type_info );

   #undef ADD_CXX_TYPEINFO_HEADER
}

